<?php
if (!defined('EZSITETPL_SOAP')) die('access violation error!');

function getAvailableTpls($ezsite_uid, $cateid, $level) {
    $tpls = array();

    $cateid = mysqli_real_escape_string($GLOBALS['db'], $cateid);
    $ezsite_uid = mysqli_real_escape_string($GLOBALS['db'], $ezsite_uid);
    $level = mysqli_real_escape_string($GLOBALS['db'], $level);

    $sql = "SELECT `ezsite_templates`.*, `ezsite_template_categories`.`name` AS category,"
        ." `purchased_templates`.`purchase_time` FROM `ezsite_template_categories`"
        ." RIGHT JOIN (`ezsite_templates` LEFT JOIN `purchased_templates` ON"
        ." `ezsite_templates`.`id`=`purchased_templates`.`ezsite_template_id`) ON"
        ." `ezsite_template_categories`.`id`=`ezsite_templates`.`ezsite_template_category_id` WHERE"
        ." (`purchased_templates`.`ezsite_uid`='$ezsite_uid' OR `ezsite_templates`.`isfree`='1')"
        ." AND `ezsite_templates`.`ezsite_template_category_id`='$cateid'"
        ." AND `ezsite_templates`.`i_level`='$level' order by i_order";
    $rs = mysqli_query($GLOBALS['db'], $sql);
    if ($rs && mysqli_num_rows($rs) > 0) {
        while ($row = mysqli_fetch_assoc($rs)) {
            $row['screenshot_url'] = PRE_SCRSHOT_URL.'/'.$row['screenshot'];
            $row['package_url'] = PRE_DOWNLOAD_URL.$row['id'];
            $tpls[] = $row;
        }
    }
    mysqli_free_result($rs);

    return serialize($tpls);
}

function masterGetAvailableTpls($cateid, $level) {
    $tpls = array();

    $cateid = mysqli_real_escape_string($GLOBALS['db'], $cateid);
    $level = mysqli_real_escape_string($GLOBALS['db'], $level);

    // Get all UID
    if (intval($cateid) == 0) {
        $sql = "SELECT `ezsite_templates`.*, `ezsite_template_categories`.`name` AS category,"
            ." `purchased_templates`.`purchase_time` FROM `ezsite_template_categories`"
            ." RIGHT JOIN (`ezsite_templates` LEFT JOIN `purchased_templates` ON"
            ." `ezsite_templates`.`id`=`purchased_templates`.`ezsite_template_id`) ON"
            ." `ezsite_template_categories`.`id`=`ezsite_templates`.`ezsite_template_category_id` WHERE"
            ." `ezsite_templates`.`i_level`='$level' ORDER BY `ezsite_templates`.`i_order`";
    } else {
        $sql = "SELECT `ezsite_templates`.*, `ezsite_template_categories`.`name` AS category,"
            ." `purchased_templates`.`purchase_time` FROM `ezsite_template_categories`"
            ." RIGHT JOIN (`ezsite_templates` LEFT JOIN `purchased_templates` ON"
            ." `ezsite_templates`.`id`=`purchased_templates`.`ezsite_template_id`) ON"
            ." `ezsite_template_categories`.`id`=`ezsite_templates`.`ezsite_template_category_id` WHERE"
            ." `ezsite_templates`.`ezsite_template_category_id`='$cateid' AND"
            ." `ezsite_templates`.`i_level`='$level' ORDER BY `ezsite_templates`.`i_order`";
    }
    $rs = mysqli_query($GLOBALS['db'], $sql);
    if ($rs && mysqli_num_rows($rs) > 0) {
        while ($row = mysqli_fetch_assoc($rs)) {
            $row['screenshot_url'] = PRE_SCRSHOT_URL.'/'.$row['screenshot'];
            $row['package_url'] = PRE_DOWNLOAD_URL.$row['id'];
            $tpls[] = $row;
        }
    }
    mysqli_free_result($rs);

    return serialize($tpls);
}

function masterGetFeaturedTpls($is_free = 0) {
    $tpls = array();
	if($is_free==1){
		 $sql = "SELECT `ezsite_templates`.*, `ezsite_template_categories`.`name` AS category,"
        ." `purchased_templates`.`purchase_time` FROM `ezsite_template_categories`"
        ." RIGHT JOIN (`ezsite_templates` LEFT JOIN `purchased_templates` ON"
        ." `ezsite_templates`.`id`=`purchased_templates`.`ezsite_template_id`) ON"
        ." `ezsite_template_categories`.`id`=`ezsite_templates`.`ezsite_template_category_id` WHERE"
        ." `ezsite_templates`.`featured`='1' and `ezsite_templates`.`is_free`='1' ORDER BY `ezsite_templates`.`i_order`";
	}else{
    $sql = "SELECT `ezsite_templates`.*, `ezsite_template_categories`.`name` AS category,"
        ." `purchased_templates`.`purchase_time` FROM `ezsite_template_categories`"
        ." RIGHT JOIN (`ezsite_templates` LEFT JOIN `purchased_templates` ON"
        ." `ezsite_templates`.`id`=`purchased_templates`.`ezsite_template_id`) ON"
        ." `ezsite_template_categories`.`id`=`ezsite_templates`.`ezsite_template_category_id` WHERE"
        ." `ezsite_templates`.`featured`='1' ORDER BY `ezsite_templates`.`i_order`";
	}
    $rs = mysqli_query($GLOBALS['db'], $sql);
    if ($rs && mysqli_num_rows($rs) > 0) {
        while ($row = mysqli_fetch_assoc($rs)) {
            $row['screenshot_url'] = PRE_SCRSHOT_URL.'/'.$row['screenshot'];
            $row['package_url'] = PRE_DOWNLOAD_URL.$row['id'];
            $tpls[] = $row;
        }
    }
    mysqli_free_result($rs);

    return serialize($tpls);
}

function getTplCategories() {
    $cates = array();

    $sql = "SELECT * FROM `ezsite_template_categories`";
    $rs = mysqli_query($GLOBALS['db'], $sql);
    if ($rs && mysqli_num_rows($rs) > 0) {
        while ($row = mysqli_fetch_assoc($rs)) {
            $cates[] = $row;
        }
    }
    mysqli_free_result($rs);

    return serialize($cates);
}

function getTplInfo($ezsite_uid, $tpl) {
    $tplinfo = array();

    /*$ezsite_uid = mysqli_real_escape_string($GLOBALS['db'], $ezsite_uid);
    $tplid = mysqli_real_escape_string($GLOBALS['db'], $tplid);

    $sql = "SELECT `ezsite_templates`.*, `purchased_templates`.`purchase_time`"
        ." FROM `ezsite_templates` LEFT JOIN `purchased_templates` ON"
        ." `ezsite_templates`.`id`=`purchased_templates`.`ezsite_template_id`"
        ." WHERE `ezsite_templates`.`id`='$tplid' AND"
        ." (`purchased_templates`.`ezsite_uid`='$ezsite_uid' OR "
        ."`ezsite_templates`.`isfree`='1')";
    $rs = mysqli_query($GLOBALS['db'], $sql);
    if ($rs && mysqli_num_rows($rs) == 1) {
        $tplinfo = mysqli_fetch_assoc($rs);
        $tplinfo['screenshot_url'] = PRE_SCRSHOT_URL.'/'.$tplinfo['screenshot'];
        $tplinfo['package_url'] = PRE_DOWNLOAD_URL.$tplinfo['id'];
    }
    mysqli_free_result($rs);*/
	$tplinfo['archive'] = $tpl;
	$tplinfo['package_url'] = PRE_DOWNLOAD_URL2.urlencode($tpl);

    return serialize($tplinfo);
}

function getTplSampleData($tpl_scalar, $level) {
    $result = 'ERROR';
    $tables_to_be_reset = array(
        'es_article_categories', 'es_articles', 'es_download_categories', 'es_downloads', 
        'es_friendlinks', 'es_galleries', 'es_menu_items', 'es_menus', 'es_module_blocks', 
        'es_navigations', 'es_online_qqs', 'es_product_categories', 'es_products', 'es_static_contents', 
        'es_templates');
    $sample_data_file = SAMPLE_DIR.'/'.$tpl_scalar.'_sample_'.$level.'.sql';
    if (!file_exists($sample_data_file)) {
        $sample_data_file = SAMPLE_DIR.'/sample.sql';
    }
    if (file_exists($sample_data_file)) {
        $fp_sample = fopen($sample_data_file, 'r');
        
        $full_sql = '';
        while (!feof($fp_sample)) {
            $line = fgets($fp_sample);
            if (preg_match('/^\-\-/', $line) 
                || preg_match('/^\/\*/', $line) 
                || preg_match('/^LOCK/', $line) 
                || preg_match('/^UNLOCK/', $line)) continue;
            $full_sql .= $line;
        }
        
        fclose($fp_sample);
        
        $tmp_sqls = preg_split('/(;\r\n)|(;\n)/s', $full_sql);
        $table_name_match = array();
        $result = array();
        foreach ($tmp_sqls as $sql) {
            if (strlen(trim($sql)) == 0) continue;
            preg_match('/`([^`]+)`/', $sql, $table_name_match);
            if (in_array($table_name_match[1], $tables_to_be_reset)) {
                $result[] = $sql;
            }
        }
    }
    
    return serialize($result);
}

function getTplSampleData_SSv2($tpl_scalar, $level) {
    $result = 'ERROR';
    $tables_to_be_reset = array(
        'es_article_categories', 'es_articles', 'es_download_categories', 'es_downloads', 
        'es_friendlinks', 'es_galleries', 'es_menu_items', 'es_menus', 'es_module_blocks', 
        'es_navigations', 'es_online_qqs', 'es_product_categories', 'es_products', 'es_static_contents', 
        'es_templates');
    $sample_data_file = SSv2_SAMPLE_DIR.'/'.$tpl_scalar.'_sample_'.$level.'.sql';
    if (!file_exists($sample_data_file)) {
        $sample_data_file = SSv2_SAMPLE_DIR.'/sample.sql';
    }
    if (file_exists($sample_data_file)) {
        $fp_sample = fopen($sample_data_file, 'r');
        
        $full_sql = '';
        while (!feof($fp_sample)) {
            $line = fgets($fp_sample);
            if (preg_match('/^\-\-/', $line) 
                || preg_match('/^\/\*/', $line) 
                || preg_match('/^LOCK/', $line) 
                || preg_match('/^UNLOCK/', $line)) continue;
            $full_sql .= $line;
        }
        
        fclose($fp_sample);
        
        $tmp_sqls = preg_split('/(;\r\n)|(;\n)/s', $full_sql);
        $table_name_match = array();
        $result = array();
        foreach ($tmp_sqls as $sql) {
            if (strlen(trim($sql)) == 0) continue;
            preg_match('/`([^`]+)`/', $sql, $table_name_match);
            if (in_array($table_name_match[1], $tables_to_be_reset)) {
                $result[] = $sql;
            }
        }
    }
    
    return serialize($result);
}

